package com.fr.lintcode;

import org.junit.Test;

public class Q276 {

    /**
     * @param s:
     *            the pearl sequences.
     * @param k:
     *            every k same pearls together will be removed.
     * @return: return the pearls after the game.
     */
    public String zumaGaming(String s, int k) {
        // write your code here.
        if (s == null || s.length() < k) {
            return s;
        }
        int count = 0;
        int left = 0;
        int index = 0;
        char current = s.charAt(left);
        while (index < s.length()) {
            if (current == s.charAt(index)) {
                count++;
                index++;
                if (count == k) {
                    s = s.substring(0, left) + s.substring(index);
                    count = 0;
                    left = 0;
                    index = 0;
                    if (s.length() == 0) {
                        return s;
                    }
                    current = s.charAt(left);
                }
            } else {
                count = 1;
                left = index;
                index++;
                current = s.charAt(left);
            }
        }
        return s;

    }

    @Test
    public void testA() {
        int k = 2;
        String s = "aaaabb";
        System.out.println(zumaGaming(s, k));
    }
}
